package mk.company.fin.frame.report.internal;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

import mk.company.fin.AppConstants;
import mk.company.fin.frame.internal.AbstractInternalFrame;
import mk.company.fin.reports.model.WarrantEntryReport;
import mk.company.fin.service.LocaleService;
import mk.company.fin.service.MessageService;
import mk.company.fin.service.ModelService;

import org.springframework.context.ApplicationContext;

import com.toedter.calendar.JDateChooser;

public class WarrantReportFrame extends AbstractInternalFrame {

	
	private static final long serialVersionUID = -3023977265495501744L;
	/* SERVICES */
	
	private MessageService messageService;
	private ModelService modelService;
	private LocaleService localeService;

	
	//component which this frame will be drawn
	JComponent component;
	public WarrantReportFrame(JComponent component) {
		this.component=component;
		
	}
	
	@Override
	public void initialize(ApplicationContext context) {
		setContext(context);
		injectServices();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(new Dimension(AppConstants.MainFrame.WIDTH, AppConstants.MainFrame.HEIGHT));
		// components
		initComponents();
	}

	/**
	 * Inject services.
	 */
	private void injectServices() {
		messageService = getContext().getBean(MessageService.class);
		modelService=getContext().getBean(ModelService.class);
		localeService=getContext().getBean(LocaleService.class);
	}
	
	public void initComponents() {
		// title
		setTitle(messageService.getMessage("warrant.report"));
		// Size
		this.setSize(AppConstants.MainFrame.WIDTH, AppConstants.MainFrame.HEIGHT);
		// Maximize
		try {
			this.setMaximum(true);
		} catch (PropertyVetoException e) {
		} catch (NullPointerException n) {
		}
		// Layout
		this.setLayout(null);
		
		initPanels();
		
		
		
		//Set the layout of all the panels
		setLayout();
		
		this.setVisible(true);
		component.add(this);
	}
	
	
	private javax.swing.JPanel datePanel;
	private JDateChooser fromDateField;
	private javax.swing.JLabel fromDateLabel;
	private JDateChooser toDateField;
	private javax.swing.JLabel toDateLabel;
	private javax.swing.JPanel warrantReportPanel;
	private javax.swing.JTable warrantReportTable;
	private javax.swing.JScrollPane warrantReportTableScrollPane;
	private javax.swing.JButton reportButton;
	    
	@SuppressWarnings("all")
	private void initPanels() {
		datePanel = new javax.swing.JPanel();
		datePanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("warrant.choose.date")));
		fromDateLabel = new javax.swing.JLabel(messageService.getMessage("warrant.report.from"));
		fromDateField = new JDateChooser();
		fromDateField.setLocale(localeService.getCurrentLocale());
		toDateLabel = new javax.swing.JLabel(messageService.getMessage("warrant.report.to"));
		toDateField = new JDateChooser();
		toDateField.setLocale(localeService.getCurrentLocale());
		warrantReportPanel = new javax.swing.JPanel();
		warrantReportPanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("warrant.list")));
		warrantReportTableScrollPane = new javax.swing.JScrollPane();
		warrantReportTable = new javax.swing.JTable();
		warrantReportTable.setModel(new javax.swing.table.DefaultTableModel(
	            new Object [][] {
	            },
	            new String [] {
	            	messageService.getMessage("warrant"),messageService.getMessage("warrant.date"),
	            	messageService.getMessage("warrant.debt.sum"),messageService.getMessage("warrant.demand.sum"),
	            	messageService.getMessage("warrant.is.equal.amount")
	            }
	        ) {
	            Class[] types = new Class [] {
	                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
	            };
	            boolean[] canEdit = new boolean [] {
	                false, false, false, false, false
	            };

	            public Class getColumnClass(int columnIndex) {
	                return types [columnIndex];
	            }

	            public boolean isCellEditable(int rowIndex, int columnIndex) {
	                return canEdit [columnIndex];
	            }
	        });
	        warrantReportTableScrollPane.setViewportView(warrantReportTable);
	        warrantReportTable.setAutoCreateRowSorter(true);
	        reportButton=new JButton();
	        reportButton.setText(messageService.getMessage("warrant.report.review"));
	        reportButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					List<WarrantEntryReport> warrantEntryList=modelService.getWarrantReport(fromDateField.getDate(), toDateField.getDate());
					((DefaultTableModel) warrantReportTable.getModel()).getDataVector().removeAllElements();
					warrantReportTable.setAutoCreateRowSorter(true);
					for (WarrantEntryReport warrantEntry:warrantEntryList){
						Vector<String> row=new Vector<String>();
						row.add(warrantEntry.getNumber());
						row.add(warrantEntry.getDate());
						row.add(warrantEntry.getSumaDolzi());
						row.add(warrantEntry.getSumaPobaruva());
						try {
							if ((Double.parseDouble(warrantEntry.getSumaDolzi())-Double.parseDouble(warrantEntry.getSumaPobaruva()))==0D){
								row.add(messageService.getMessage("yes"));		
							}
							else
								row.add(messageService.getMessage("no"));
						}
						catch (Exception ex) {};
						((DefaultTableModel) warrantReportTable.getModel()).addRow(row);
					}
				}
			});
	}
	private void setLayout(){
		javax.swing.GroupLayout datePanelLayout = new javax.swing.GroupLayout(datePanel);
        datePanel.setLayout(datePanelLayout);
        datePanelLayout.setHorizontalGroup(
            datePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(datePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(fromDateLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fromDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(toDateLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(toDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(reportButton)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        datePanelLayout.setVerticalGroup(
            datePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(datePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(datePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fromDateLabel)
                    .addComponent(fromDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toDateLabel)
                    .addComponent(toDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reportButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        javax.swing.GroupLayout warrantReportPanelLayout = new javax.swing.GroupLayout(warrantReportPanel);
        warrantReportPanel.setLayout(warrantReportPanelLayout);
        warrantReportPanelLayout.setHorizontalGroup(
            warrantReportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(warrantReportPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(warrantReportTableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                .addContainerGap())
        );
        warrantReportPanelLayout.setVerticalGroup(
            warrantReportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(warrantReportPanelLayout.createSequentialGroup()
                .addComponent(warrantReportTableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(warrantReportPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(datePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(datePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(warrantReportPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
        );

	}
	@Override
	public void draw() {
		injectServices();
		initComponents();
	}

}
